package cn.micro.eye.web;

import cn.micro.eye.commons.auth.MicroToken;
import cn.micro.eye.entity.Account;
import cn.micro.eye.mapper.IAccountMapper;
import cn.micro.eye.mapper.IMemberMapper;
import cn.micro.eye.model.add.AccountRegister;
import cn.micro.eye.model.query.AccountLogin;
import cn.micro.eye.service.IAccountService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 账号表，Account-Member为1-1 前端控制器
 * </p>
 *
 * @author lry
 * @since 2018-10-07
 */
@Validated
@RestController
@RequestMapping("account")
public class AccountController {

    @Resource
    private IAccountService accountService;
    @Resource
    private IAccountMapper accountMapper;
    @Resource
    private IMemberMapper memberMapper;

    @RequestMapping(value = "queryOneById/{id}", method = RequestMethod.GET)
    public List<Account> queryOneById(@PathVariable("id") Long id) throws Exception {
        return accountService.listEqAndIn(Account::getOpenId, id, Account::getRegisterType, 2);
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public Boolean register(@RequestBody @Validated AccountRegister accountRegister) throws Exception {
        return accountService.addRegister(accountRegister);
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public MicroToken login(@RequestBody @Validated AccountLogin accountLogin) throws Exception {
        return accountService.queryLogin(accountLogin);
    }

}
